//
//  ViewController.m
//  XQJFCityMacDemo
//
//  Created by WXQ on 2019/11/7.
//  Copyright © 2019 WXQ. All rights reserved.
//

#import "ViewController.h"
#import <FMDB/FMDB.h>


@interface ViewController ()

@property (nonatomic, strong) FMDatabase *db;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

#pragma mark - responds

- (IBAction)respondsToCreateDB:(id)sender {
    [self areaSqliteDBData];
}

- (IBAction)respondsToWriteDB:(id)sender {
    [self save];
}

- (IBAction)respondsToClearDB:(id)sender {
    
}

- (void)areaSqliteDBData {
    if (self.db) {
        return;
    }
    
    // copy"area.sqlite"到Documents中
//    NSFileManager *fileManager = [NSFileManager defaultManager];
//    NSError *error;
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *documentsDirectory = [paths objectAtIndex:0];
//    NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"shop_area.sqlite"];
//    if([fileManager fileExistsAtPath:txtPath] == NO) {
//        // wxq_因为封装成库了, shop_area.sqlite 自然也在库中, 这里取的bundle, 就应该是库中的
//        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"shop_area" ofType:@"sqlite"];
//        [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
//    }
    
    
    // 新建数据库并打开
    NSString *path  = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"shop_area.sqlite"];
    NSLog(@"path: %@", path);
    FMDatabase *db = [FMDatabase databaseWithPath:path];
    self.db = db;
    BOOL success = [db open];
    if (success) {
        // 数据库创建成功!
        NSLog(@"数据库打开成功!");
        NSString *sqlStr = @"CREATE TABLE IF NOT EXISTS shop_area (area_number INTEGER ,area_name TEXT ,city_number INTEGER ,city_name TEXT ,province_number INTEGER ,province_name TEXT);";
        BOOL successT = [self.db executeUpdate:sqlStr];
        if (successT) {
            // 创建表成功!
            NSLog(@"创建表成功!");
        }else{
            // 创建表失败!
            NSLog(@"创建表失败!");
            [self.db close];
        }
    }else{
        // 数据库创建失败!
        NSLog(@"数据库创建失败!");
        [self.db close];
    }
}

- (void)save {
    if (!self.db) {
        return;
    }
    
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"pca-code" ofType:@"json"];
    
    NSError *error = nil;
    
//    NSString *json = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    
//    NSArray *json = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@", json);
    
    
    
    NSMutableArray *muArr = [NSMutableArray array];
    
    for (NSDictionary *province in json) {
        
        NSString *province_code = province[@"code"];
        NSString *province_name = province[@"name"];
        NSArray *cityArr = province[@"children"];
//        "code": "11",
//        "name": "北京市",
//        "children":[]
        
        for (NSDictionary *city in cityArr) {
            
            NSString *city_code = city[@"code"];
            NSString *city_name = city[@"name"];
            NSArray *areaArr = city[@"children"];
            
            for (NSDictionary *area in areaArr) {
                
                NSString *area_code = area[@"code"];
                NSString *area_name = area[@"name"];
                
                NSDictionary *dataDic = @{
                    @"area_number": @([area_code intValue]),
                    @"area_name": area_name,
                    @"city_number": @(city_code.intValue),
                    @"city_name": city_name,
                    @"province_number": @(province_code.intValue),
                    @"province_name": province_name,
                };
                
//                [muArr addObject:dataDic];
                
                NSString *keys = [dataDic.allKeys componentsJoinedByString:@","];
                NSString *values = [dataDic.allValues componentsJoinedByString:@"','"];
                values = [NSString stringWithFormat:@"'%@'", values];
                NSString *formatStr = [NSString stringWithFormat:@"insert into shop_area (%@) values(%@)", keys, values];
                [self.db executeUpdate:formatStr];
                
            }
            
        }
    }
    
}


@end
